{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "provenance": []
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "source": [
        "# **Reflexion**\n",
        "\n",
        "Reflexion is a novel framework that introduces verbal reinforcement learning for language agents, allowing them to learn from past experiences and improve decision-making through self-reflection. Unlike traditional reinforcement learning (RL), which relies on extensive training and parameter updates, Reflexion enhances agent performance using natural language-based self-improvement without modifying model weights.\n",
        "\n",
        "### Key Concepts of Reflexion:\n",
        "1. **Verbal Reinforcement Learning**: Instead of numerical rewards, Reflexion agents receive textual feedback in the form of self-reflections, helping them identify mistakes and refine future decisions.\n",
        "2. **Episodic Memory for Self-Learning**: Reflexion maintains a memory buffer where past self-reflections are stored, enabling the agent to recall past mistakes and refine its approach dynamically.\n",
        "3. **Three-Component Framework**:\n",
        "   - **Actor**: The LLM agent that performs tasks and generates outputs.\n",
        "   - **Evaluator**: Assesses the performance of the agent’s output, providing feedback.\n",
        "   - **Self-Reflection Model**: Generates verbal reinforcement signals that help the agent improve future decisions.\n",
        "4. **Flexible Feedback Mechanisms**:\n",
        "   - Reflexion supports multiple forms of feedback, including binary signals, scalar values, heuristic assessments, and self-generated evaluations.\n",
        "   - It enables learning in various environments such as programming, decision-making, and reasoning tasks.\n",
        "5. **Enhancing Performance Across Tasks**:\n",
        "   - **Decision-Making**: Reflexion improves multi-step reasoning by refining choices through iterative self-evaluation.\n",
        "   - **Programming**: It employs self-generated unit tests to refine and debug code, achieving **91% pass@1 accuracy** on HumanEval, outperforming GPT-4’s 80%.\n",
        "   - **Language Reasoning**: Reflexion boosts performance in reasoning-intensive tasks such as HotpotQA by incorporating iterative improvements based on verbal feedback.\n",
        "\n",
        "### Advantages of Reflexion:\n",
        "- **Eliminates the Need for Model Fine-Tuning**: Unlike standard RL methods that require costly fine-tuning, Reflexion refines outputs without updating model parameters.\n",
        "- **Improves Generalization**: Reflexion enables agents to learn from diverse failures and adapt across different domains, from programming to multi-step decision-making.\n",
        "- **More Efficient Learning**: By replacing trial-and-error learning with self-reflection, Reflexion **reduces sample complexity**, requiring fewer attempts to improve performance.\n",
        "- **Enhanced Interpretability**: The explicit self-reflection process allows users to **track and analyze** the agent’s learning path, improving trust and reliability in AI decision-making.\n",
        "\n",
        "By introducing self-reflective feedback loops, Reflexion represents a major step toward autonomous, adaptive, and interpretable AI agents. This paradigm shift moves beyond static model predictions toward agents that can critically assess and refine their own outputs, bringing us closer to more effective AI-driven problem-solving across multiple domains.\n",
        "\n",
        "paper link: https://arxiv.org/pdf/2303.11366\n",
        "\n",
        "Code Reference: https://github.com/langchain-ai/langgraph/blob/main/docs/docs/tutorials/reflexion/reflexion.ipynb\n",
        "\n",
        "![3.png]()"
      ],
      "metadata": {
        "id": "Adfg4N9_OcQG"
      }
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "0ae516BWNQkT"
      },
      "outputs": [],
      "source": [
        "!pip install --q langgraph langchain_openai langchain_community"
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "import os\n",
        "from google.colab import userdata\n",
        "os.environ['OPENAI_API_KEY'] = userdata.get('OPENAI_API_KEY')\n",
        "os.environ[\"TAVILY_API_KEY\"] = userdata.get('TAVILY_API_KEY')"
      ],
      "metadata": {
        "id": "8v9C20gVNd8w"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "from langchain_openai import ChatOpenAI\n",
        "llm = ChatOpenAI(model=\"gpt-4o\")"
      ],
      "metadata": {
        "id": "djBN8Qx6Nbe7"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "# Set up Tavily Web Search API\n",
        "from langchain_community.tools.tavily_search import TavilySearchResults\n",
        "tavily_tool = TavilySearchResults(max_results=5)"
      ],
      "metadata": {
        "id": "JpYiAfShNmj3"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "from langchain_core.messages import HumanMessage, ToolMessage\n",
        "from langchain_core.output_parsers.openai_tools import PydanticToolsParser\n",
        "from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder\n",
        "from pydantic import ValidationError\n",
        "\n",
        "from pydantic import BaseModel, Field\n",
        "\n",
        "\n",
        "class Reflection(BaseModel):\n",
        "    missing: str = Field(description=\"Critique of what is missing.\")\n",
        "    superfluous: str = Field(description=\"Critique of what is superfluous\")\n",
        "\n",
        "\n",
        "class AnswerQuestion(BaseModel):\n",
        "    \"\"\"Answer the question. Provide an answer, reflection, and then follow up with search queries to improve the answer.\"\"\"\n",
        "\n",
        "    answer: str = Field(description=\"~250 word detailed answer to the question.\")\n",
        "    reflection: Reflection = Field(description=\"Your reflection on the initial answer.\")\n",
        "    search_queries: list[str] = Field(\n",
        "        description=\"1-3 search queries for researching improvements to address the critique of your current answer.\"\n",
        "    )\n",
        "\n",
        "\n",
        "class ResponderWithRetries:\n",
        "    def __init__(self, runnable, validator):\n",
        "        self.runnable = runnable\n",
        "        self.validator = validator\n",
        "\n",
        "    def respond(self, state: dict):\n",
        "        response = []\n",
        "        # Convert the initial state dictionary to a list of messages\n",
        "        messages = state[\"messages\"]\n",
        "        for attempt in range(3):\n",
        "            response = self.runnable.invoke(\n",
        "                {\"messages\": messages}, {\"tags\": [f\"attempt:{attempt}\"]}\n",
        "            )\n",
        "            try:\n",
        "                self.validator.invoke(response)\n",
        "                return {\"messages\": response}\n",
        "            except ValidationError as e:\n",
        "                # Append new messages to the existing list\n",
        "                messages.extend(\n",
        "                    [\n",
        "                        response,\n",
        "                        ToolMessage(\n",
        "                            content=f\"{repr(e)}\\n\\nPay close attention to the function schema.\\n\\n\"\n",
        "                            + self.validator.schema_json()\n",
        "                            + \" Respond by fixing all validation errors.\",\n",
        "                            tool_call_id=response.tool_calls[0][\"id\"],\n",
        "                        ),\n",
        "                    ]\n",
        "                )\n",
        "                # Update the state with the modified list of messages\n",
        "                state[\"messages\"] = messages\n",
        "\n",
        "        return {\"messages\": response}"
      ],
      "metadata": {
        "id": "BtkOCM6UNtfY"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "import datetime\n",
        "\n",
        "actor_prompt_template = ChatPromptTemplate.from_messages(\n",
        "    [\n",
        "        (\n",
        "            \"system\",\n",
        "            \"\"\"You are expert researcher.\n",
        "Current time: {time}\n",
        "\n",
        "1. {first_instruction}\n",
        "2. Reflect and critique your answer. Be severe to maximize improvement.\n",
        "3. Recommend search queries to research information and improve your answer.\"\"\",\n",
        "        ),\n",
        "        MessagesPlaceholder(variable_name=\"messages\"),\n",
        "        (\n",
        "            \"user\",\n",
        "            \"\\n\\n<system>Reflect on the user's original question and the\"\n",
        "            \" actions taken thus far. Respond using the {function_name} function.</reminder>\",\n",
        "        ),\n",
        "    ]\n",
        ").partial(\n",
        "    time=lambda: datetime.datetime.now().isoformat(),\n",
        ")\n",
        "initial_answer_chain = actor_prompt_template.partial(\n",
        "    first_instruction=\"Provide a detailed ~250 word answer.\",\n",
        "    function_name=AnswerQuestion.__name__,\n",
        ") | llm.bind_tools(tools=[AnswerQuestion])\n",
        "validator = PydanticToolsParser(tools=[AnswerQuestion])\n",
        "\n",
        "first_responder = ResponderWithRetries(\n",
        "    runnable=initial_answer_chain, validator=validator\n",
        ")"
      ],
      "metadata": {
        "id": "xtPE1-pSNv9s"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "example_question = \"Why is reflection useful in AI?\"\n",
        "initial = first_responder.respond(\n",
        "    {\"messages\": [HumanMessage(content=example_question)]}\n",
        ")"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "RGYK89pzNyQU",
        "outputId": "eca55468-ff03-43fb-af70-972070dbc0ea"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stderr",
          "text": [
            "<ipython-input-5-11906d12f482>:47: PydanticDeprecatedSince20: The `schema_json` method is deprecated; use `model_json_schema` and json.dumps instead. Deprecated in Pydantic V2.0 to be removed in V3.0. See Pydantic V2 Migration Guide at https://errors.pydantic.dev/2.10/migration/\n",
            "  + self.validator.schema_json()\n",
            "<ipython-input-5-11906d12f482>:47: PydanticDeprecatedSince20: The `schema_json` method is deprecated; use `model_json_schema` and json.dumps instead. Deprecated in Pydantic V2.0 to be removed in V3.0. See Pydantic V2 Migration Guide at https://errors.pydantic.dev/2.10/migration/\n",
            "  + self.validator.schema_json()\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "revise_instructions = \"\"\"Revise your previous answer using the new information.\n",
        "    - You should use the previous critique to add important information to your answer.\n",
        "        - You MUST include numerical citations in your revised answer to ensure it can be verified.\n",
        "        - Add a \"References\" section to the bottom of your answer (which does not count towards the word limit). In form of:\n",
        "            - [1] https://example.com\n",
        "            - [2] https://example.com\n",
        "    - You should use the previous critique to remove superfluous information from your answer and make SURE it is not more than 250 words.\n",
        "\"\"\"\n",
        "\n",
        "\n",
        "# Extend the initial answer schema to include references.\n",
        "# Forcing citation in the model encourages grounded responses\n",
        "class ReviseAnswer(AnswerQuestion):\n",
        "    \"\"\"Revise your original answer to your question. Provide an answer, reflection,\n",
        "\n",
        "    cite your reflection with references, and finally\n",
        "    add search queries to improve the answer.\"\"\"\n",
        "\n",
        "    references: list[str] = Field(\n",
        "        description=\"Citations motivating your updated answer.\"\n",
        "    )\n",
        "\n",
        "\n",
        "revision_chain = actor_prompt_template.partial(\n",
        "    first_instruction=revise_instructions,\n",
        "    function_name=ReviseAnswer.__name__,\n",
        ") | llm.bind_tools(tools=[ReviseAnswer])\n",
        "revision_validator = PydanticToolsParser(tools=[ReviseAnswer])\n",
        "\n",
        "revisor = ResponderWithRetries(runnable=revision_chain, validator=revision_validator)"
      ],
      "metadata": {
        "id": "3keWaN0QN3YG"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "import json\n",
        "\n",
        "revised = revisor.respond(\n",
        "    {\n",
        "        \"messages\": [\n",
        "            HumanMessage(content=example_question),\n",
        "            initial[\"messages\"],\n",
        "            ToolMessage(\n",
        "                tool_call_id=initial[\"messages\"].tool_calls[0][\"id\"],\n",
        "                content=json.dumps(\n",
        "                    tavily_tool.invoke(\n",
        "                        {\n",
        "                            \"query\": initial[\"messages\"].tool_calls[0][\"args\"][\n",
        "                                \"search_queries\"\n",
        "                            ][0]\n",
        "                        }\n",
        "                    )\n",
        "                ),\n",
        "            ),\n",
        "        ]\n",
        "    }\n",
        ")\n",
        "revised[\"messages\"]"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "-jMrdpNVN5ay",
        "outputId": "71b4f0d6-3c98-46b9-adba-71b493d419e6"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "AIMessage(content='', additional_kwargs={'tool_calls': [{'id': 'call_GEwvkL477bOgsdqS1LonAxbq', 'function': {'arguments': '{\"answer\": \"Reflection in AI is crucial for enhancing performance, learning, decision-making, and safety. It enables AI systems to evaluate past actions and outcomes, enhancing their ability to adjust strategies and improve future performance. Through iterative feedback loops and memory mechanisms, AI can self-evaluate and refine its outputs, similar to human learning processes [1][2].\\\\n\\\\nReflection is instrumental in debugging AI systems by allowing models to identify and learn from their mistakes. This helps in pinpointing algorithmic misinterpretations and making precise corrections [3]. Furthermore, reflection enhances the transparency and interpretability of AI decision-making, which is crucial for developers and users to understand the rationale behind AI actions [4].\\\\n\\\\nMoreover, it contributes to AI ethics and safety by reviewing actions against ethical guidelines to ensure moral compliance and reduce biases [5]. An example of reflection in practice is in AI trading algorithms, where reflection enhances decision-making by revising logic and strategies based on market feedback [2].\", \"reflection\": {\"missing\": \"The answer could benefit from additional concrete examples or case studies illustrating reflection in AI systems. Specific real-life applications would make the explanation more relatable and comprehensive.\", \"superfluous\": \"The initial comparison to human learning wasn\\'t well-received as certain readers may not need this analogy to understand AI reflection\\'s technical benefits.\"}, \"search_queries\": [\"importance of reflection in AI systems\", \"examples of reflection in artificial intelligence\", \"case studies on AI reflection and decision-making\"], \"references\": [\"https://www.linkedin.com/pulse/importance-self-reflection-artificial-intelligence-del-wester-uh6hc\", \"https://www.akira.ai/blog/reflection-agent-prompting\", \"https://medium.com/@krtarunsingh/a-new-era-of-ai-learning-reflexions-path-to-smarter-systems-49bf6a5881c3\", \"https://www.servicenow.com/community/intelligence-ml-blog/the-power-of-self-reflection-in-an-ai-driven-world/ba-p/3100439\", \"https://www.psychologytoday.com/us/blog/connecting-with-coincidence/202408/ai-bots-have-some-degree-of-self-reflection\"]}', 'name': 'ReviseAnswer'}, 'type': 'function'}], 'refusal': None}, response_metadata={'token_usage': {'completion_tokens': 458, 'prompt_tokens': 1766, 'total_tokens': 2224, 'completion_tokens_details': {'accepted_prediction_tokens': 0, 'audio_tokens': 0, 'reasoning_tokens': 0, 'rejected_prediction_tokens': 0}, 'prompt_tokens_details': {'audio_tokens': 0, 'cached_tokens': 0}}, 'model_name': 'gpt-4o-2024-08-06', 'system_fingerprint': 'fp_5e115050d0', 'finish_reason': 'tool_calls', 'logprobs': None}, id='run-152295b3-030f-4d76-bf15-3d3a7b84d5d1-0', tool_calls=[{'name': 'ReviseAnswer', 'args': {'answer': 'Reflection in AI is crucial for enhancing performance, learning, decision-making, and safety. It enables AI systems to evaluate past actions and outcomes, enhancing their ability to adjust strategies and improve future performance. Through iterative feedback loops and memory mechanisms, AI can self-evaluate and refine its outputs, similar to human learning processes [1][2].\\n\\nReflection is instrumental in debugging AI systems by allowing models to identify and learn from their mistakes. This helps in pinpointing algorithmic misinterpretations and making precise corrections [3]. Furthermore, reflection enhances the transparency and interpretability of AI decision-making, which is crucial for developers and users to understand the rationale behind AI actions [4].\\n\\nMoreover, it contributes to AI ethics and safety by reviewing actions against ethical guidelines to ensure moral compliance and reduce biases [5]. An example of reflection in practice is in AI trading algorithms, where reflection enhances decision-making by revising logic and strategies based on market feedback [2].', 'reflection': {'missing': 'The answer could benefit from additional concrete examples or case studies illustrating reflection in AI systems. Specific real-life applications would make the explanation more relatable and comprehensive.', 'superfluous': \"The initial comparison to human learning wasn't well-received as certain readers may not need this analogy to understand AI reflection's technical benefits.\"}, 'search_queries': ['importance of reflection in AI systems', 'examples of reflection in artificial intelligence', 'case studies on AI reflection and decision-making'], 'references': ['https://www.linkedin.com/pulse/importance-self-reflection-artificial-intelligence-del-wester-uh6hc', 'https://www.akira.ai/blog/reflection-agent-prompting', 'https://medium.com/@krtarunsingh/a-new-era-of-ai-learning-reflexions-path-to-smarter-systems-49bf6a5881c3', 'https://www.servicenow.com/community/intelligence-ml-blog/the-power-of-self-reflection-in-an-ai-driven-world/ba-p/3100439', 'https://www.psychologytoday.com/us/blog/connecting-with-coincidence/202408/ai-bots-have-some-degree-of-self-reflection']}, 'id': 'call_GEwvkL477bOgsdqS1LonAxbq', 'type': 'tool_call'}], usage_metadata={'input_tokens': 1766, 'output_tokens': 458, 'total_tokens': 2224, 'input_token_details': {'audio': 0, 'cache_read': 0}, 'output_token_details': {'audio': 0, 'reasoning': 0}})"
            ]
          },
          "metadata": {},
          "execution_count": 9
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "from langchain_core.tools import StructuredTool\n",
        "\n",
        "from langgraph.prebuilt import ToolNode\n",
        "\n",
        "\n",
        "def run_queries(search_queries: list[str], **kwargs):\n",
        "    \"\"\"Run the generated queries.\"\"\"\n",
        "    return tavily_tool.batch([{\"query\": query} for query in search_queries])\n",
        "\n",
        "\n",
        "tool_node = ToolNode(\n",
        "    [\n",
        "        StructuredTool.from_function(run_queries, name=AnswerQuestion.__name__),\n",
        "        StructuredTool.from_function(run_queries, name=ReviseAnswer.__name__),\n",
        "    ]\n",
        ")"
      ],
      "metadata": {
        "id": "nMdV8ST5N9UC"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "from typing import Literal\n",
        "\n",
        "from langgraph.graph import END, StateGraph, START\n",
        "from langgraph.graph.message import add_messages\n",
        "from typing import Annotated\n",
        "from typing_extensions import TypedDict\n",
        "\n",
        "\n",
        "class State(TypedDict):\n",
        "    messages: Annotated[list, add_messages]\n",
        "\n",
        "\n",
        "MAX_ITERATIONS = 5\n",
        "builder = StateGraph(State)\n",
        "builder.add_node(\"draft\", first_responder.respond)\n",
        "\n",
        "\n",
        "builder.add_node(\"execute_tools\", tool_node)\n",
        "builder.add_node(\"revise\", revisor.respond)\n",
        "# draft -> execute_tools\n",
        "builder.add_edge(\"draft\", \"execute_tools\")\n",
        "# execute_tools -> revise\n",
        "builder.add_edge(\"execute_tools\", \"revise\")\n",
        "\n",
        "# Define looping logic:\n",
        "\n",
        "\n",
        "def _get_num_iterations(state: list):\n",
        "    i = 0\n",
        "    for m in state[::-1]:\n",
        "        if m.type not in {\"tool\", \"ai\"}:\n",
        "            break\n",
        "        i += 1\n",
        "    return i\n",
        "\n",
        "\n",
        "def event_loop(state: list):\n",
        "    # in our case, we'll just stop after N plans\n",
        "    num_iterations = _get_num_iterations(state[\"messages\"])\n",
        "    if num_iterations > MAX_ITERATIONS:\n",
        "        return END\n",
        "    return \"execute_tools\"\n",
        "\n",
        "\n",
        "# revise -> execute_tools OR end\n",
        "builder.add_conditional_edges(\"revise\", event_loop, [\"execute_tools\", END])\n",
        "builder.add_edge(START, \"draft\")\n",
        "graph = builder.compile()"
      ],
      "metadata": {
        "id": "Kunb950NN_oI"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "events = graph.stream(\n",
        "    {\"messages\": [(\"user\", \"How should we handle the climate crisis?\")]},\n",
        "    stream_mode=\"values\",\n",
        ")\n",
        "for i, step in enumerate(events):\n",
        "    print(f\"Step {i}\")\n",
        "    step[\"messages\"][-1].pretty_print()"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "JNW6BwmYOB4G",
        "outputId": "e80be7e4-951c-4757-f0ce-bcdb39f64f3a"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Step 0\n",
            "================================\u001b[1m Human Message \u001b[0m=================================\n",
            "\n",
            "How should we handle the climate crisis?\n"
          ]
        },
        {
          "output_type": "stream",
          "name": "stderr",
          "text": [
            "<ipython-input-5-11906d12f482>:47: PydanticDeprecatedSince20: The `schema_json` method is deprecated; use `model_json_schema` and json.dumps instead. Deprecated in Pydantic V2.0 to be removed in V3.0. See Pydantic V2 Migration Guide at https://errors.pydantic.dev/2.10/migration/\n",
            "  + self.validator.schema_json()\n"
          ]
        },
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Step 1\n",
            "==================================\u001b[1m Ai Message \u001b[0m==================================\n",
            "Tool Calls:\n",
            "  AnswerQuestion (call_qETkkv74NYDKWj86AuTDf4Ek)\n",
            " Call ID: call_qETkkv74NYDKWj86AuTDf4Ek\n",
            "  Args:\n",
            "    answer: Addressing the climate crisis requires a multifaceted approach that involves governments, businesses, communities, and individuals working together to mitigate and adapt to the adverse effects of climate change. Firstly, international cooperation is paramount, emphasizing the strengthening of global agreements like the Paris Agreement, which aims to limit global warming to well below 2 degrees Celsius above pre-industrial levels. Policymaking should foster sustainable energy transition, prioritizing investments in renewable energy sources (such as solar and wind), and phasing out fossil fuels.\n",
            "\n",
            "On a national and local level, governments can introduce policies that incentivize green innovation, enforce emissions regulations, and implement carbon pricing mechanisms to discourage carbon-intensive practices. It's also essential to invest in resilient infrastructure to withstand extreme weather events, which are increasingly prevalent due to climate change.\n",
            "\n",
            "Businesses need to adopt sustainable practices, reducing their carbon footprint through energy-efficient technologies and supply chain sustainability. Emphasizing a circular economy, where waste is minimized, and resources are reused, can also play a vital role in reducing emissions.\n",
            "\n",
            "At the community and individual levels, increasing awareness and education about sustainable practices can drive behavioral changes. This includes promoting energy conservation, sustainable agriculture, reduced meat consumption, and reforestation efforts.\n",
            "\n",
            "Adopting these strategies collectively can significantly contribute to managing the climate crisis, but it requires concerted effort, substantial financial investment, and a commitment to long-term sustainability goals.\n",
            "    reflection: {'missing': 'The answer could improve by providing more specific examples of successful initiatives or policies already implemented in various parts of the world. Additionally, it could explore potential barriers to these solutions and how they might be overcome. Finally, including the role of technology and innovation in addressing climate change would create a more comprehensive answer.', 'superfluous': 'The answer contains general statements that, while accurate, may not add specific value or insight into the multifaceted approach mentioned. It would benefit from cutting down on broadly defined concepts and focusing instead on detailed examples and more nuanced discussion of each point.'}\n",
            "    search_queries: ['successful climate change mitigation policies', 'technology innovation in climate solution', 'barriers to implementing climate policies']\n",
            "Step 2\n",
            "=================================\u001b[1m Tool Message \u001b[0m=================================\n",
            "Name: AnswerQuestion\n",
            "\n",
            "[[{\"url\": \"https://www.sciencedirect.com/science/article/pii/S1364032120308868\", \"content\": \"pillar_safe A review of successful climate change mitigation policies in major emitting economies and the potential of global replication - ScienceDirect A review of successful climate change mitigation policies in major emitting economies and the potential of global replication Global replication of sector policies would reduce emissions by 20% in 2030. The article further presents an explorative emissions scenario developed under the assumption that all countries will replicate both the observed trends in sector-level indicators and the trends that policies for future emissions reductions aspire to achieve. It shows that the global replication of sector progress would reduce greenhouse gas emissions by 2030 by about 20% compared to a current policies scenario. For all open access content, the relevant licensing terms apply.\"}, {\"url\": \"https://www.worldbank.org/en/news/feature/2023/09/19/climate-policies-with-real-world-results\", \"content\": \"pillar_safe The World Bank’s Reality Check report highlights 25 successful climate policies from countries as disparate as Egypt, Niger, China, and Peru. A new World Bank report, Reality Check: Lessons from 25 Policies Advancing a Low-Carbon Future showcases examples across sectors and five continents, from countries as disparate as Egypt, Niger, China and Peru. “They provide invaluable insights on how countries actually design and implement climate policies, and on the hard compromises that doing so can require, such as the rapid expansion of solar power in India, the use of waste to generate affordable energy in Mexico, and the greening of Colombia’s construction industry.”\"}, {\"url\": \"https://css.umich.edu/publications/factsheets/climate-change/climate-change-policy-and-mitigation-factsheet\", \"content\": \"Climate Change: Policy and Mitigation Factsheet » Climate Change: Policy and Mitigation Factsheet » The objective of the United Nations Framework Convention on Climate Change (UNFCCC), which virtually all  nations, including the U.S., have ratified, is to stabilize greenhouse gas (GHG) concentrations at a level that will not cause “dangerous anthropogenic (human-induced) interference with the climate system.”1 Due to the persistence of some GHGs in the atmosphere, significant emissions reductions must be achieved in coming decades to meet the UNFCCC objective. Voluntary agreements are generally made between a government agency and one or more private parties to “achieve environmental objectives or to improve environmental performance beyond compliance.”12 EPA partners with various sectors to oversee a variety of voluntary programs aimed at reducing GHG emissions, increasing clean energy adoption, and adapting to climate change.13 Center for Climate and Energy Solutions (2023) U.S. State Greenhouse Gas Emissions Targets.\"}, {\"url\": \"https://www.eea.europa.eu/en/topics/in-depth/climate-change-mitigation-reducing-emissions\", \"content\": \"These include targets for greenhouse gas emissions in key sectors of the economy, promoting the use of renewable energy and low carbon fuels, energy efficiency improvements in buildings, and many more. Most EU sectors reduced greenhouse gas emissions over the past three decades, with the highest reductions in the energy supply sector. Total net greenhouse gas emissions in the European Union dropped by 8% last year, marking significant progress towards climate neutrality for the EU. In 2023, EU total net greenhouse gas emissions decreased to 37% below 1990 levels according to preliminary figures in the EEA Trends and Projections report. Europe's greenhouse gas data: explore emissions, projections sectors, countries and much more...\"}, {\"url\": \"https://climatepromise.undp.org/news-and-stories/what-climate-change-mitigation-and-why-it-urgent\", \"content\": \"UNDP is also supporting small island developing states like the Comoros to invest in renewable energy and sustainable infrastructure. What is UNDP’s role in supporting climate change mitigation? UNDP aims to assist countries with their climate change mitigation efforts, guiding them towards sustainable, low-carbon and climate-resilient development. Specifically, UNDP’s offer of support includes developing and improving legislation and policy, standards and regulations, capacity building, knowledge dissemination, and financial mobilization for countries to pilot and scale-up mitigation solutions such as renewable energy projects, energy efficiency initiatives and sustainable land-use practices. With financial support from the Global Environment Facility and the Green Climate Fund, UNDP has an active portfolio of 94 climate change mitigation projects in 69 countries.\"}], [{\"url\": \"https://www.perchenergy.com/blog/innovation/climate-technology-solving-environmental-challenges-examples\", \"content\": \"Climate technology, also known as clean technology or green technology, is a range of innovative solutions aiming to reduce greenhouse gas emissions, promote renewable energy sources, enhance energy efficiency, and address various aspects of climate change. Climate technology offers innovative and sustainable alternatives that can significantly reduce emissions and transition toward a low-carbon economy. By deploying renewable energy sources, improving energy efficiency, and adopting sustainable practices in various sectors, climate technology offers the means to significantly decrease carbon dioxide and other greenhouse gas emissions, thereby slowing global warming. Also, advancements in climate technology are driving down costs, making clean energy and sustainable solutions increasingly competitive and financially viable. The increasing global demand for clean energy, sustainable transportation, and efficient resource management will drive investments in climate technology.\"}, {\"url\": \"https://ecomatcher.medium.com/seeding-the-future-how-innovation-in-climate-tech-is-redefining-green-solutions-5ccc264f7f62\", \"content\": \"Climate technology encompasses a broad range of tools and innovations designed to mitigate the impacts of climate change, enhance resilience, and drive\"}, {\"url\": \"https://www.weforum.org/stories/2024/02/ai-climate-adaptation-technologies/\", \"content\": \"pillar_safe 6 technologies to help the world adapt to climate change | World Economic Forum 6 technologies to help the world adapt to climate change Six technologies are critical for climate adaptation, according to the World Economic Forum’s new report, Innovation and Adaptation in the Climate Crisis. In a new report, Innovation and Adaptation in the Climate Crisis: Technology for the New Normal, the World Economic Forum looks at six data-driven and digital technologies that can play a “mission-critical” role in global climate adaptation. Drones can help organizations adapt to climate change by collecting visual data on climate risk and impacts. Early warning systems powered by technologies including earth observation and drones can help save lives in climate disasters.\"}, {\"url\": \"https://www.wipo.int/green-technology-book-mitigation/en/technology-and-innovation-for-climate-mitigation/index.html\", \"content\": \"The International Finance Corporation estimates there is a climate investment opportunity in emerging market cities amounting to USD 29.4 trillion by 2030.[12] This relates to six main sectors, many of which are covered in the Green Technology Book, namely: waste, climate-smart water, renewable energy, electric vehicles, public transport and green buildings. Global patent applications for climate change mitigation technologies – a key measure of innovation – are trending down. Available at: https://www.iea.org/commentaries/global-patent-applications-for-climate-change-mitigation-technologies-a-key-measure-of-innovation-are-trending-down Global patent applications for climate change mitigation technologies – a key measure of innovation – are trending down. Available at: https://www.iea.org/commentaries/global-patent-applications-for-climate-change-mitigation-technologies-a-key-measure-of-innovation-are-trending-down\"}, {\"url\": \"https://www.imf.org/en/Publications/fandd/issues/2021/09/bezos-earth-fund-climate-change-innovation-levin\", \"content\": \"pillar_safe IMF reports and publications by country IMF Regional Reports About the IMF IMF Finances IMF NOTES There has also been innovation in the economic understanding of climate change. The International Energy Agency’s (IEA’s) new net zero roadmap notes that the needed decarbonization by 2030 is largely achievable with readily available technologies, but by mid-century almost half of required emissions reductions will call for technologies that are not yet on the market. Scaling capture and storage will not only rely upon technological innovation to reduce energy inputs and costs, it will also depend on policy support such as tax credits, greater market demand, and public and private investment, among other factors.\"}], [{\"url\": \"https://www.sciencedirect.com/science/article/pii/S2212096322000213\", \"content\": \"Lack of resources, fragmentation, and lack of awareness and communication are the most commonly identified barriers to national adaptation policy. We also find\"}, {\"url\": \"https://pmc.ncbi.nlm.nih.gov/articles/PMC10557327/\", \"content\": \"Key barriers were insufficient staff, resources and lack of support from management/leaders, and lack of local evidence. Conclusion. To mitigate\"}, {\"url\": \"https://ourworld.unu.edu/en/political-barriers-to-climate-change-adaptation\", \"content\": \"The difficulty in effectively implementing concrete policy measures was usually explained by a range of limiting factors such as ecological (natural constraints)\"}, {\"url\": \"https://www.climatepolicyinitiative.org/gca-africa-adaptation-finance/barriers/regulatory-and-policy-barriers/\", \"content\": \"Absence of climate resilience mandate: · Lack of enabling environment: · Lack of regulatory incentives: · Lack of subnational fiscal autonomy: · Misaligned\"}, {\"url\": \"http://climatepolicyinfohub.eu/climate-change-adaptation-needs-barriers-and-limits.html\", \"content\": \"Since public and private actors are involved in the implementation of adaptation measures, decision-making barriers may reduce the desired level of adaptation.\"}]]\n"
          ]
        },
        {
          "output_type": "stream",
          "name": "stderr",
          "text": [
            "<ipython-input-5-11906d12f482>:47: PydanticDeprecatedSince20: The `schema_json` method is deprecated; use `model_json_schema` and json.dumps instead. Deprecated in Pydantic V2.0 to be removed in V3.0. See Pydantic V2 Migration Guide at https://errors.pydantic.dev/2.10/migration/\n",
            "  + self.validator.schema_json()\n"
          ]
        },
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Step 3\n",
            "==================================\u001b[1m Ai Message \u001b[0m==================================\n",
            "Tool Calls:\n",
            "  ReviseAnswer (call_8ahftUxkJ3jWFivpTSkTZibA)\n",
            " Call ID: call_8ahftUxkJ3jWFivpTSkTZibA\n",
            "  Args:\n",
            "    answer: Addressing the climate crisis requires a multifaceted approach involving international, national, and individual efforts. Strengthening global agreements such as the Paris Agreement is crucial. This agreement aims to limit global warming to below 2°C above pre-industrial levels through national commitments and fostering sustainable energy transitions. Nations should focus on renewable energy investments and phasing out fossil fuels. For instance, India's rapid solar power expansion and Mexico's waste-to-energy initiatives showcase successful policies. [1][2]\n",
            "\n",
            "National policies should promote green innovation and impose carbon pricing to deter carbon-intensive practices. Businesses must adopt sustainable practices, like energy-efficient technologies and supply chain sustainability, adopting a circular economy to minimize waste. Communities and individuals can drive change by promoting energy conservation and sustainable agriculture.\n",
            "\n",
            "Technological advancements in climate technology, such as improved renewable energy solutions and enhanced energy efficiency, are pivotal in reducing emissions [3][4]. However, barriers such as lack of regulatory incentives and limited resources can impede policy implementation [5][6]. Overcoming these barriers involves addressing misaligned incentives and bolstering capacity-building efforts.\n",
            "\n",
            "Collective efforts with sustained investment in technology and policy innovation are vital to overcoming these challenges and achieving long-term climate goals. Although difficult, the synergistic application of these strategies offers a path forward in tackling the climate crisis.\n",
            "    reflection: {'missing': 'The revised answer focuses on specifics, including successful policies worldwide like solar energy in India and waste-to-energy in Mexico. It also highlights barriers to policy implementation and the role of technology, providing a more comprehensive view. However, more detailed examples of local community efforts and individual actions could enhance the response further.', 'superfluous': 'The revised response efficiently removes overly broad statements, enhancing focus on specific initiatives and technological solutions. Yet, it could still trim down on general process descriptions and instead prioritize concrete impacts of the policies mentioned.'}\n",
            "    search_queries: ['successful community climate initiatives', 'individual actions in climate change mitigation', 'overcoming climate policy barriers']\n",
            "    references: ['https://www.worldbank.org/en/news/feature/2023/09/19/climate-policies-with-real-world-results', 'https://www.eea.europa.eu/en/topics/in-depth/climate-change-mitigation-reducing-emissions', 'https://www.perchenergy.com/blog/innovation/climate-technology-solving-environmental-challenges-examples', 'https://www.wipo.int/green-technology-book-mitigation/en/technology-and-innovation-for-climate-mitigation/index.html', 'https://www.sciencedirect.com/science/article/pii/S2212096322000213', 'https://climatepolicyinfohub.eu/climate-change-adaptation-needs-barriers-and-limits.html']\n"
          ]
        }
      ]
    }
  ]
}